var models = require('../db')
var express = require('express')
var router = express.Router()
var mysql = require('mysql')
var $sql = require('../sqlMap')

var conn = mysql.createConnection(models.mysql)

conn.connect()
var jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
        code: '1',
        msg: '操作失败'
        })
    } else {
        res.json(ret)
    }
}

router.get('/getOrderList',(req,res) =>{
    var params = req.query || req.params
    let start = (params.page - 1) * 10;
    var sql = $sql.order.count;
    let allCount = 0;
    conn.query(sql,function(err,result){
        if(err){
            console.log(err);
        }else{
            console.log(result)
            allCount = result[0]['count(*)'];
            sql = $sql.order.get;
            conn.query(sql,[start],function(err,result){
                if(err){
                    console.log(err);
                    throw err;
                }else{
                    var list = result;
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:list
                    }))
                }
            })
        }
    })
})

router.get('/searchOrderList',(req,res) =>{
    var params = req.query || req.params
    let start = (params.page - 1) * 10;
    var sql = $sql.order.searchCount;
    let search = ""
    if(params.user_id){
        search +=`user_id=${parseInt(params.user_id)}`
        //search.user_id = parseInt(params.user_id)
    }
    if(params.flight_id){
        search +=` and flight_id=${parseInt(params.flight_id)}`
        //search.flight_id = parseInt(params.flight_id)
    }
    let allCount = 0;
    conn.query(sql+search,function(err,result){
        if(err){
            console.log(err);
        }else{
            console.log(result)
            allCount = result[0]['count(*)'];
            sql = $sql.order.searchList;
            let limit = $sql.order.limit
            conn.query(sql+search+limit,[start],function(err,searchResult){
                if(err){
                    console.log(err);
                    throw err;
                }else{
                    var list = searchResult;
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:list
                    }))
                }
            })
        }
    })
})

router.post('/addOrder',(req,res)=>{
    var sql = $sql.order.add
    var params = req.body
    params.create_time = new Date();
    params.modify_time = new Date();
    params.is_del = 0;
    params.user_id = parseInt(params.user_id);
    params.admin_id = parseInt(params.admin_id);
    params.flight_id = parseInt(params.flight_id);
    params.touristClassNum = parseInt(params.touristClassNum);
    params.businessClassNum = parseInt(params.businessClassNum);
    params.firstClassNum = parseInt(params.firstClassNum);
    params.totalPrice = parseFloat(params.totalPrice);
    console.log(params)
    conn.query(sql, [params], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log(result)
            sql = $sql.order.addOrder
            conn.query(sql,[params.touristClassNum,params.businessClassNum,params.firstClassNum,params.flight_id],function (err, addResult){
                if (err) {
                    console.log(err)
                }
                if(addResult){
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        order_id:result.insertId
                    }))
                }
            })
        }
    })
})

router.post('/deleteOrder',(req,res)=>{
    var sql = $sql.order.update
    var params = req.body
    params.modify_time = new Date();
    params.is_del = 1;
    params.admin_id = parseInt(params.admin_id);
    params.order_id = parseInt(params.order_id);
    params.flight_id = parseInt(params.flight_id);
    params.touristClassNum = parseInt(params.touristClassNum);
    params.businessClassNum = parseInt(params.businessClassNum);
    params.firstClassNum = parseInt(params.firstClassNum);
    console.log(params)
    conn.query(sql, [params,params.order_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            sql = $sql.order.delOrder
            conn.query(sql,[params.touristClassNum,params.businessClassNum,params.firstClassNum,params.flight_id],function (err, delResult){
                if (err) {
                    console.log(err)
                }
                if(delResult){
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200
                    }))
                }
            })
        }
    })
})

module.exports = router;